﻿Namespace Core.Extensibility
    Public Class Packaging

        Private __MainScriptFile As String

        Public ReadOnly Property MainScriptFile As String
            Get
                Return __MainScriptFile
            End Get
        End Property

        Public Sub UnpackageExtension(ByVal ExtensionFile As String,
                                                  ByVal ProgressEvent As [Delegate])

            Dim ScriptsDir As New IO.DirectoryInfo(StardustDir & "\Scripts")
            'Check if Scripts Dir exists
            If ScriptsDir.Exists = False Then
                'if not exists, create it
                ScriptsDir.Create()
            End If


            Dim ZipFile As String = ExtensionFile
            'Console.WriteLine("Extracting file {0} to {1}", ZipFile, ScriptsDir.FullName)

            Dim MainScriptFile As String = ""

            Using zip1 As Ionic.Zip.ZipFile = Ionic.Zip.ZipFile.Read(ZipFile)
                'Add the handler here

                AddHandler zip1.ExtractProgress, ProgressEvent

                Dim Entry As Ionic.Zip.ZipEntry

                For Each Entry In zip1
                    Entry.Extract(ScriptsDir.FullName, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
                    Dim _fi As New IO.FileInfo(Entry.FileName)
                    If _fi.Extension = ".cf" Then
                        MainScriptFile = String.Format("{0}\{1}", ScriptsDir.FullName, _fi.Name)
                    End If
                Next
            End Using

            __MainScriptFile = MainScriptFile

        End Sub


    End Class

End Namespace